package com.ljh.service.impl;

import com.ljh.pojo.User;
import com.ljh.mapper.UserMapper;
import com.ljh.service.USERervice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.utils.MD5Utils;
import com.ljh.utils.ValidatorUtil;
import com.ljh.vo.LoginVo;
import com.ljh.vo.RespBean;
import com.ljh.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-09-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements USERervice {

    @Autowired
    private UserMapper userMapper;

    @Override
    public RespBean doLogin(HttpSession session, LoginVo loginVo) {

        String mobile = loginVo.getMobile();
        String formPass = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(formPass)) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if (!ValidatorUtil.isMobile(mobile)) {
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }
//        判断是否存在
        User user = userMapper.selectMobile(Long.parseLong(mobile));
        System.out.println(user);
        session.setAttribute("Username",user.getUsername());
        session.setAttribute("UsernameID",user.getId());
        if (user == null) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if (!MD5Utils.fromPassToDBPass(formPass, user.getSlat()).equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        return RespBean.success();
    }
}